﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class FLTICCGABEWReport : BaseReport
    {
        string strCompany = "HLT";
        string strExcelFile = "";
        string sheetName = "";
        string Standard = "GBMOV";
        string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\HLT\HLTPlanningReport.xls");
        public FLTICCGABEWReport()
            : base()
        {
        }
        public FLTICCGABEWReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            strExcelFile = @"FLT\FLTInventory.xls";
            /*
             A.ICCGABDAT,A.ICCWH,A.ICCGABTYPE,A.ICCGABPARTIE,A.ICCGABSPPART,A.ICCGABFBPART,
A.ICCGABZWPART,A.ICCGABCODE,A.ICCGABNAME,A.ICCGABDES,A.ICCGABLO,A.ICCGABLB,
A.ICCGABLBEHI,A.ICCGABART,A.ICCGABVAR,A.ICCGSGARNBEZ,A.ICCGSAGARNNR,A.ICCGABFARBE,
A.ICCGSCBEZ,A.ICCGABAUFKRS,A.ICCGABAUFLFD,A.ICCGABAUFPOS,A.ICCGABKG*/
            #region Excel  columns
            columns.Add("ICCGABDAT");
            columns.Add("ICCWH");
            columns.Add("ICCGABTYPE");
            columns.Add("ICCGABPARTIE");
            columns.Add("ICCGABSPPART");
            columns.Add("ICCGABFBPART");
            columns.Add("ICCGABZWPART");
            columns.Add("ICCGABCODE");
            columns.Add("ICCGABNAME");
            columns.Add("ICCGABDES");
            columns.Add("ICCGABLO");
            columns.Add("ICCGABLB");
            columns.Add("ICCGABLBEHI");
            columns.Add("ICCGABART");
            columns.Add("ICCGABVAR");
            columns.Add("ICCGSGARNBEZ");
            columns.Add("ICCGSAGARNNR");
            columns.Add("ICCGABFARBE");
            columns.Add("ICCGSCBEZ");
            columns.Add("ICCGABAUFKRS");
            columns.Add("ICCGABAUFLFD");
            columns.Add("ICCGABAUFPOS");
            columns.Add("ICCGABKG");
            
            #endregion 
        }
        public void FillInExcelData(ArrayList columns, DataSet setSoruce, int xStart = 7, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["ICC_GABEW"];
                DataTable dtOutput = new DataTable();
                dtOutput.Columns.Add("ICCGABDAT", typeof(System.String));
                dtOutput.Columns.Add("ICCWH", typeof(System.String));
                dtOutput.Columns.Add("ICCGABTYPE", typeof(System.String));
                dtOutput.Columns.Add("ICCGABPARTIE", typeof(System.String));
                dtOutput.Columns.Add("ICCGABSPPART", typeof(System.String));
                dtOutput.Columns.Add("ICCGABFBPART", typeof(System.String));
                dtOutput.Columns.Add("ICCGABZWPART", typeof(System.String));
                dtOutput.Columns.Add("ICCGABCODE", typeof(System.String));
                dtOutput.Columns.Add("ICCGABNAME", typeof(System.String));
                dtOutput.Columns.Add("ICCGABDES", typeof(System.String));
                dtOutput.Columns.Add("ICCGABLO", typeof(System.String));
                dtOutput.Columns.Add("ICCGABLB", typeof(System.String));
                dtOutput.Columns.Add("ICCGABLBEHI", typeof(System.String));
                dtOutput.Columns.Add("ICCGABART", typeof(System.String));
                dtOutput.Columns.Add("ICCGABVAR", typeof(System.String));
                dtOutput.Columns.Add("ICCGSGARNBEZ", typeof(System.String));
                dtOutput.Columns.Add("ICCGSAGARNNR", typeof(System.String));
                dtOutput.Columns.Add("ICCGABFARBE", typeof(System.String));
                dtOutput.Columns.Add("ICCGSCBEZ", typeof(System.String));
                dtOutput.Columns.Add("ICCGABAUFKRS", typeof(System.String));
                dtOutput.Columns.Add("ICCGABAUFLFD", typeof(System.String));
                dtOutput.Columns.Add("ICCGABAUFPOS", typeof(System.String));
                dtOutput.Columns.Add("ICCGABKG", System.Type.GetType("System.Decimal"));
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);

                exAgent.DataTableToExcel(dtOutput, 4, 1);
                int count = dtOutput.Rows.Count;
                exAgent.BatchConvert2Number(4, 23, count + 3, 23);
                exAgent.SetCellValue(1, 1, " ");
                /*foreach (DataRow dataRow in dtSoruce.Rows)
                {
                    y = yStart;
                    foreach (string column in columns)
                    {
                        exAgent.SetCellValue(x, y, dataRow[column].ToString());
                        y++;
                    }
                    x++;
                }*/ 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void WriteExcelReport(string fileType, string STARTDATE, string ENDDATE)
        {
            try
            {
                string filename = "";
                string sheetName = "";
                string Standard = "ICCGABEW";
                strExcelFile = @"FLT\FLTStandard.xls";
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\" + strExcelFile);

                exAgent.Open(filePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                filename = @"Financial\业务明细数据_" + filedate + ".xls";
                string file = Path.Combine(PathHelper.ApplicationPath, @"Report\" + filename);
                sheetName = DateTime.Now.ToShortTimeString().Replace(@":", "");
                if (System.IO.File.Exists(file))
                {
                    System.IO.File.Delete(file);
                }
                exAgent.CreateNewSheet(filename, Standard, sheetName);

                exAgent.SetCellValue(1, 3, STARTDATE + "-" + ENDDATE);
                FillInExcelData(columns, dsSource, 4, 1);
                exAgent.Save();
                exAgent.Close();
                System.Diagnostics.Process.Start("EXCEL.EXE", file);
                //base.WriteExcelReport("OpenExcel");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
